package com.app.ytong.camera;

import android.app.Service;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;

public class SlientTackPicService extends Service {

    private static final String TAG = "slient_tack_pic";

    private static final int SLIENT_TACK_CAMERA = 1;

    private SlientCamera slientCamera;

    private int cameraStatus;

    private int replySN;

    private String replySEID;

    public MyHandler handler = new MyHandler(this);

    private static class MyHandler extends Handler {
        WeakReference<SlientTackPicService> mContext;

        MyHandler(SlientTackPicService mContext) {
            this.mContext = new WeakReference<SlientTackPicService>(mContext);
        }

        @Override
        public void handleMessage(Message msg) {
            SlientTackPicService service = mContext.get();
            if (null == service) {
                Log.e(TAG, "mContext.get() is null!");
                return;
            }
            switch (msg.what) {
                case SLIENT_TACK_CAMERA:
                    Log.i(TAG, "camera ready, tackPicture!");
                    service.slientCamera.tackPicture();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        slientCamera = new SlientCamera(this);
        slientCamera.onCreate();

        // 1是前置摄像头  0 是后置摄像头
        cameraStatus = slientCamera.openCamere() ? Camera.CameraInfo.CAMERA_FACING_FRONT : Camera.CameraInfo.CAMERA_FACING_BACK;
        Log.i(TAG, "cameraStatus = " + cameraStatus);
//        if (cameraStatus == 1) {
//            handler.sendEmptyMessageDelayed(SLIENT_TACK_CAMERA, 300);
//        }
    }

// EventBus.getDefault().post(CaptureMsg()) 来启动

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnMessage() {
        slientCamera = new SlientCamera(this);
        slientCamera.onCreate();

        // 1是前置摄像头  0 是后置摄像头
        cameraStatus = slientCamera.openCamere() ? Camera.CameraInfo.CAMERA_FACING_FRONT : Camera.CameraInfo.CAMERA_FACING_BACK;
//        if (cameraStatus == 1) {
        handler.sendEmptyMessageDelayed(SLIENT_TACK_CAMERA, 300);
//        }
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void releaseSource() {
        Log.i(TAG, "releaseSource");
        if (handler.hasMessages(SLIENT_TACK_CAMERA)) {
            handler.removeMessages(SLIENT_TACK_CAMERA);
        }
        stopSelf();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "SlientTackPicService, onDestroy()");
        releaseSource();
        slientCamera.onDestroy();
    }
}
